home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d20 / dorskel2.arc / XBBSMSG.ARC / MAKEPKT.C < prev    next >
C/C++ Source or Header  |  1991-12-05  |  4KB  |  164 lines

  1. #ifndef __TURBOC__
  2.     #define INCL_DOS
  3.     #include <os2.h>            /* for DosSleep() */
  4.     #include <sys/locking.h>
  5. #endif
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <io.h>
  9. #include <fcntl.h>
  10. #include <share.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <errno.h>
  15. #include "xmsg.h"
  16. #include "pkt.h"
  17.  
  18. #ifdef __TURBOC__
  19.     #define LK_LOCK     1
  20.     #define LK_UNLCK    2
  21.     #define DosSleep(x) sleep(1)
  22.     int pascal locking (int handle,int type,long length);  
  23.     #define _fastcall pascal
  24. #endif
  25.  
  26. /* Simple packet building functions than can be used for rescanning,
  27.    building mailerless point packets, etc. */
  28.  
  29. /* global variables created here */
  30.  
  31. int  pkthandle = -1;        /* current packet handle */
  32. char currout[257] = "";    /* name of last packet opened */
  33.  
  34.  
  35.  
  36. void _fastcall close_pkt (void) {
  37.  
  38.     /* close the global packet handle */
  39.  
  40.     if(pkthandle != -1) {
  41.         close(pkthandle);
  42.         pkthandle = -1;
  43.         *currout = 0;
  44.     }
  45. }
  46.  
  47.  
  48. int _fastcall open_pkt (char *filename,AN_ADDR *hisaddr,AN_ADDR *myaddr) {
  49.  
  50.     long        pos;
  51.     PKTHDR      ph;
  52.  
  53.  
  54.     /* Open (create if necessary) packet requested.
  55.        Return global file handle for the packet positioned to eop.
  56.        Note that use of a global file handle allows us to speed up
  57.        the packet-writing process dramatically and simplify coding
  58.        in general, but that it makes this code non-reentrant.
  59.        Returns -1 on error, pkthandle on success. */
  60.  
  61.     if(!filename || !filename || !hisaddr || !myaddr)
  62.       return -1;   /* error in parameters */
  63.  
  64.     if(pkthandle != -1 && !stricmp(filename,currout)
  65.       return pkthandle;  /* this file already open */
  66.  
  67.     if(pkthandle != -1) {  /* close previous packet */
  68.         close_pkt();
  69.     }
  70.  
  71.     pkthandle = sopen(filename,O_RDWR | O_BINARY | O_CREAT,SH_DENYWR,S_IREAD | S_IWRITE);
  72.     if(pkthandle != -1) {
  73.         lseek(pkthandle,0L,SEEK_END);
  74.         if(!tell(pkthandle)) {
  75.             memset(&ph,0,sizeof(PKTHDR));
  76.             ph.version = 2;
  77.             ph.subver = 2;
  78.             ph.dnode  = addr->node;
  79.             ph.dnet   = addr->net;
  80.             ph.dzone  = addr->zone;
  81.             ph.dpoint = addr->point;
  82.             strncpy(ph.ddomain,addr->domain,8);
  83.             maddr = best_guess(addr,myaddr);
  84.             ph.onode  = maddr->node;
  85.             ph.onet   = maddr->net;
  86.             ph.ozone  = maddr->zone;
  87.             ph.opoint = maddr->point;
  88.             strncpy(ph.odomain,maddr->domain,8);
  89.             write(pkthandle,&ph,sizeof(PKTHDR));
  90.             write(pkthandle,"\0",2);
  91.         }
  92.         pos = tell(pkthandle);
  93.         if(pos) lseek(pkthandle,pos - 2L,SEEK_SET);  /* Position to next msg spot */
  94.         else {                  /* error */
  95.             close_pkt();
  96.             unlink(filename);
  97.             return -1;
  98.         }
  99.     }
  100.  
  101.     strcpy(currout,filename);
  102.     return pkthandle;
  103. }
  104.  
  105.  
  106.  
  107.  
  108. int _fastcall write_pkt_msg (int fp,XMSG *amsg,char *text,char *area) {
  109.  
  110.     char pmsg[192],*p;
  111.     unsigned int x;
  112.  
  113.  
  114.     /* Write the message given to the end of packet handle fp as a
  115.        packed msg.  Always returns 1; not much error-checking. */
  116.  
  117.     memset(pmsg,192,0);
  118.     *pmsg = 0x02;
  119.     pmsg[1] = 0x00;
  120.     memcpy(&pmsg[2],&amsg->orig,2);
  121.     memcpy(&pmsg[4],&amsg->dest,2);
  122.     memcpy(&pmsg[6],&amsg->orig_net,2);
  123.     memcpy(&pmsg[8],&amsg->dest_net,2);
  124.     memcpy(&pmsg[10],&amsg->attr,2);
  125.     x = 0;
  126.     memcpy(&pmsg[12],&x,2);
  127.     for(x = 0;x < 20;x++) if(!amsg->date[x]) amsg->date[x] = 'Q';
  128.     amsg->date[19] = 0;
  129.     memcpy(&pmsg[14],amsg->date,20);    
  130.     p = &pmsg[34];
  131.     strcpy(p,amsg->to);
  132.     x = 34;
  133.     while(*p){
  134.         x++;
  135.         p++;
  136.     }
  137.     p++;
  138.     x++;
  139.     strcpy(p,amsg->from);
  140.     while(*p){
  141.         x++;
  142.         p++;
  143.     }
  144.     x++;
  145.     p++;
  146.     strcpy(p,amsg->subj);
  147.     while(*p){
  148.         x++;
  149.         p++;
  150.     }
  151.     x++;
  152.     p++;
  153.     write(fp,pmsg,x);
  154.     if(area && *area) {                  /* Prepend area tag */
  155.         write(fp,"AREA: ",6);
  156.         write(fp,area,strlen(area));
  157.         write(fp,"\r",1);
  158.     }
  159.     write(fp,text,strlen(text));
  160.     write(fp,"\0\0",3);
  161.     lseek(fp,(tell(fp) - 2L),SEEK_SET);  /* Ready for another msg */
  162.     return 1;
  163. }
  164.